package com.datareport.data.controller;


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.datareport.data.Utils.DisabilityInfoPastListener;
import com.datareport.data.dto.ResultDTO;
import com.datareport.data.entity.DisabilityInfoPast;
import com.datareport.data.mapper.DisabilityInfoPastMapper;
import com.datareport.data.service.impl.DisabilityInfoPastServiceImpl;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author DataReport
 * @since 2021-11-18
 */
@RestController
public class DisabilityInfoPastController {
    @Autowired
    private DisabilityInfoPastServiceImpl disabilityInfoPastService;

    @Autowired
    DisabilityInfoPastMapper disabilityInfoPastMapper;

    @ApiOperation("Excel文件上传接口")
    @PostMapping("/import/past")
    public ResultDTO importFile(@RequestParam("file") MultipartFile file) {

        ExcelReader excelReader =null;
        try {
            excelReader= EasyExcel.read(file.getInputStream(), DisabilityInfoPast.class, new DisabilityInfoPastListener(disabilityInfoPastService)).headRowNumber(2).build();
            ReadSheet readSheet = EasyExcel.readSheet(0).build();
            excelReader.read(readSheet);
        } catch (IOException e) {
            if (excelReader != null) {

                // 这里需要关闭，读的时候会创建临时文件，磁盘压力大
                excelReader.finish();
            }

        }
        return ResultDTO.okOf();
    }

    /**
     *
     * @param pageNum 请求的数据页数
     * @param pageSize 请求数据条数
     * @return
     */
    @ApiOperation("查询所有信息接口")
    @GetMapping("/disabilityInfoPast/selectAll")
    public IPage<DisabilityInfoPast> selectDisabilityInfoPage(@RequestParam("pageNum") Integer pageNum,
                                                              @RequestParam("pageSize") Integer pageSize) {

        Page<DisabilityInfoPast> pastPage = disabilityInfoPastService.selectAll(pageNum, pageSize);

        return pastPage;
    }

}
